package com.xh.clean.modular.clean.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xh.clean.core.base.controller.BaseController;
import com.xh.clean.core.common.annotion.BussinessLog;
import com.xh.clean.core.common.constant.dictmap.DeleteDict;
import com.xh.clean.core.common.constant.dictmap.ToiletDict;
import com.xh.clean.core.common.constant.factory.ConstantFactory;
import com.xh.clean.core.log.LogObjectHolder;
import com.xh.clean.core.shiro.ShiroKit;
import com.xh.clean.modular.clean.model.Toilet;
import com.xh.clean.modular.clean.service.IStoreyService;
import com.xh.clean.modular.clean.service.IToiletService;
import com.xh.clean.modular.clean.warpper.ToiletWarpper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 卫生间操作控制器
 *
 * @author yangjie
 * @Date 2018-06-23 16:38:48
 */
@Controller
@RequestMapping("/toilet")
public class ToiletController extends BaseController {

    private String PREFIX = "/clean/toilet/";

    @Autowired
    private IToiletService toiletService;
    @Autowired
    private IStoreyService storeyService;

    /**
     * 跳转到卫生间操作首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "toilet.html";
    }

    /**
     * 跳转到添加卫生间操作
     */
    @RequestMapping("/toilet_add")
    public String toiletAdd(Model model) {
        List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);
        return PREFIX + "toilet_add.html";
    }

    /**
     * 跳转到修改卫生间操作
     */
    @RequestMapping("/toilet_update/{toiletId}")
    public String toiletUpdate(@PathVariable Integer toiletId, Model model) {
        Toilet toilet = toiletService.selectById(toiletId);
        model.addAttribute("item", toilet);
        List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);
        LogObjectHolder.me().set(toilet);
        return PREFIX + "toilet_edit.html";
    }

    /**
     * 获取卫生间操作列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String toiletName, @RequestParam(required = false) String position, @RequestParam(required = false) Integer storeyId) {
        Wrapper<Toilet> wrapper = new EntityWrapper<Toilet>();
        if (StringUtils.isNotEmpty(toiletName)) {
            wrapper.eq("name", toiletName);
        }
        if (StringUtils.isNotEmpty(position)) {
            wrapper.eq("position", position);
        }
        if (storeyId != null) {
            wrapper.eq("storey_id", storeyId);
        }
        List<Map<String, Object>> toilets = toiletService.selectMaps(wrapper);
        return super.warpObject(new ToiletWarpper(toilets));
    }

    /**
     * 新增卫生间操作
     *
     * @param toilet
     * @return
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "添加卫生间", key = "storeyId,position,name", dict = ToiletDict.class)
    @ResponseBody
    public Object add(Toilet toilet) {
        toilet.setCreatetime(new Date());
        toilet.setCreater(ShiroKit.getUser().getId());
        toiletService.insert(toilet);
        return SUCCESS_TIP;
    }

    /**
     * 删除卫生间操作
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除卫生间", key = "toiletId", dict = DeleteDict.class)
    @ResponseBody
    public Object delete(@RequestParam Integer toiletId) {
        LogObjectHolder.me().set(ConstantFactory.me().getToiletNameById(toiletId));
        toiletService.deleteById(toiletId);
        return SUCCESS_TIP;
    }

    /**
     * 修改卫生间操作
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改卫生间", key = "storeyId,position,name", dict = ToiletDict.class)
    @ResponseBody
    public Object update(Toilet toilet) {
        toiletService.updateById(toilet);
        return SUCCESS_TIP;
    }

    /**
     * 卫生间操作详情
     */
    @RequestMapping(value = "/detail/{toiletId}")
    @ResponseBody
    public Object detail(@PathVariable("toiletId") Integer toiletId) {
        return toiletService.selectById(toiletId);
    }
}
